package com.jxfgyj.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.jxfgyj.domain.DO.jxfgyj_styleEvaluation;
import com.jxfgyj.domain.DO.jxfgyj_teacher;
import com.jxfgyj.domain.DO.jxfgyj_user;
import com.jxfgyj.domain.DO.jxfgyj_video;
import com.jxfgyj.domain.DTO.TeacherStatistics;
import com.jxfgyj.domain.DTO.VideoDTO;
import com.jxfgyj.service.VideoService;
import com.jxfgyj.util.UuidUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class VideoAction extends ActionSupport implements ServletResponseAware, ServletRequestAware {

	private HttpServletResponse http_response;
	private HttpServletRequest http_request;
	/*
	 * 
	 */
	private VideoService videoService;
	/*
	 * 
	 */
	private File videofile;
	private String videofileFileName;
	private String videofileContentType;
	/*
	 * 
	 * 
	 */
	private InputStream inputStream;
	/*
	 *  
	 */
	private jxfgyj_video video;
	private jxfgyj_teacher teacher;
	private String search;
	private String filter_teacher = "-1";
	private String filter_evaluation = "-1";
	private jxfgyj_styleEvaluation styleEvaluation;

	/*
	 * 
	 */
	public String VideoManage() {
		return "VideoManage";
	}

	public String VideoStyleEvaluation() {
		return "VideoStyleEvaluation";
	}

	/*
	 * 
	 */
	public void addVideo() {

		video.setVideo_id(UuidUtil.getUuid());
		/*
		 * 加载配置文件中指定的存放路径
		 */
		String lj = "";
		try {
			Properties props = new Properties();
			props.load(this.getClass().getClassLoader().getResourceAsStream("file.properties"));
			lj = props.getProperty("lj");
		} catch (Exception e) {
			e.printStackTrace();
		}
		/*
		 * 存储视频
		 */
		if (videofile != null) {
			String filePath;
			String fileName = video.getVideo_id() + videofileFileName.substring(videofileFileName.lastIndexOf("."));
			filePath = lj + "video/" + fileName;
			video.setVideo_name(fileName);
			File newFile = new File(filePath);
			try {
				FileUtils.copyFile(videofile, newFile);
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {
			video.setVideo_name("none");
		}
		/*
		 * 
		 */
		videoService.saveVideo(video);
		/*
		 * 
		 */
		http_response.setContentType("text/html;charset=utf-8");
		try {
			http_response.getWriter().write("1");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void listVideo() {
		GsonBuilder gsonBuilder = new GsonBuilder();
		gsonBuilder.setPrettyPrinting();// 格式化json数据
		Gson gson = gsonBuilder.serializeNulls().create();
		//
		http_response.setContentType("text/html;charset=utf-8");
		List<jxfgyj_video> listVideo = videoService.listVideo(search);
		try {
			http_response.getWriter().write(gson.toJson(listVideo));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void getTeacherStatistics() {
		GsonBuilder gsonBuilder = new GsonBuilder();
		gsonBuilder.setPrettyPrinting();// 格式化json数据
		Gson gson = gsonBuilder.serializeNulls().create();
		http_response.setContentType("text/html;charset=utf-8");
		//
		TeacherStatistics teacherStatistics = videoService.getTeacherStatistics(video);
		try {
			http_response.getWriter().write(gson.toJson(teacherStatistics));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void listVideoDTO() {
		GsonBuilder gsonBuilder = new GsonBuilder();
		gsonBuilder.setPrettyPrinting();// 格式化json数据
		Gson gson = gsonBuilder.serializeNulls().create();
		http_response.setContentType("text/html;charset=utf-8");
		//
		jxfgyj_user loginUser = (jxfgyj_user) ActionContext.getContext().getSession().get("loginResult");
		if (loginUser != null) {
			List<VideoDTO> listVideoDTO = videoService.listVideoDTO(loginUser, filter_teacher, filter_evaluation,
					search);
			try {
				http_response.getWriter().write(gson.toJson(listVideoDTO));
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}

	/**
	 * 删除
	 */
	public void deleteVideo() {
		videoService.deleteVideo(video.getVideo_id());

		http_response.setContentType("text/html;charset=utf-8");
		try {
			http_response.getWriter().write("1");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void deleteEvaluation() {
		videoService.deleteEvaluation(styleEvaluation.getStyleEvaluation_id());

		http_response.setContentType("text/html;charset=utf-8");
		try {
			http_response.getWriter().write("1");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public String playVideo() throws FileNotFoundException {
		/*
		 * 加载配置文件中指定的存放路径
		 */
		String lj = "";
		try {
			Properties props = new Properties();
			props.load(this.getClass().getClassLoader().getResourceAsStream("file.properties"));
			lj = props.getProperty("lj");
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (video == null || video.getVideo_name().equals("")) {
			return "";
		}
		File file = new File(lj + "video/" + video.getVideo_name());
		try {
			inputStream = new FileInputStream(file);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			// file = new File("C://wlmtxt/video/NotFound.jpg");
			inputStream = new FileInputStream(file);
		}

		return "getFile";
	}

	public void evaluationVideo() {
		jxfgyj_user loginUser = (jxfgyj_user) ActionContext.getContext().getSession().get("loginResult");
		styleEvaluation.setStyleEvaluation_user(loginUser.getUser_id());
		videoService.evaluationVideo(styleEvaluation);

		/*
		 * 
		 */
		http_response.setContentType("text/html;charset=utf-8");
		try {
			http_response.getWriter().write("1");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/*
	 * 
	 */
	@Override
	public void setServletRequest(HttpServletRequest http_request) {
		this.http_request = http_request;
	}

	@Override
	public void setServletResponse(HttpServletResponse http_response) {
		this.http_response = http_response;

	}

	public HttpServletResponse getHttp_response() {
		return http_response;
	}

	public void setHttp_response(HttpServletResponse http_response) {
		this.http_response = http_response;
	}

	public HttpServletRequest getHttp_request() {
		return http_request;
	}

	public void setHttp_request(HttpServletRequest http_request) {
		this.http_request = http_request;
	}

	/*
	 * 
	 */
	public VideoService getVideoService() {
		return videoService;
	}

	public void setVideoService(VideoService videoService) {
		this.videoService = videoService;
	}

	public File getVideofile() {
		return videofile;
	}

	public void setVideofile(File videofile) {
		this.videofile = videofile;
	}

	public String getVideofileFileName() {
		return videofileFileName;
	}

	public void setVideofileFileName(String videofileFileName) {
		this.videofileFileName = videofileFileName;
	}

	public String getVideofileContentType() {
		return videofileContentType;
	}

	public void setVideofileContentType(String videofileContentType) {
		this.videofileContentType = videofileContentType;
	}

	public jxfgyj_video getVideo() {
		return video;
	}

	public void setVideo(jxfgyj_video video) {
		this.video = video;
	}

	public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}

	public String getSearch() {
		return search;
	}

	public void setSearch(String search) {
		this.search = search;
	}

	public jxfgyj_styleEvaluation getStyleEvaluation() {
		return styleEvaluation;
	}

	public void setStyleEvaluation(jxfgyj_styleEvaluation styleEvaluation) {
		this.styleEvaluation = styleEvaluation;
	}

	public String getFilter_teacher() {
		return filter_teacher;
	}

	public void setFilter_teacher(String filter_teacher) {
		this.filter_teacher = filter_teacher;
	}

	public String getFilter_evaluation() {
		return filter_evaluation;
	}

	public void setFilter_evaluation(String filter_evaluation) {
		this.filter_evaluation = filter_evaluation;
	}

	public jxfgyj_teacher getTeacher() {
		return teacher;
	}

	public void setTeacher(jxfgyj_teacher teacher) {
		this.teacher = teacher;
	}

	/*
	 * 
	 */

}
